package org.example.springboot.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherApiClient {

    private static final String URL = "http://v1.yiketianqi.com/api?unescape=1&version=v63&appid=81191881&appsecret=J6qFGxVz&city=巴彦淖尔";

    public static void main(String[] args) {
        try {
            //String response = sendGetRequest(URL);
            Map<String, Object> jsonMap = new HashMap<>();
            jsonMap.put("username", "smartet");
            jsonMap.put("password", "123456");

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(jsonMap);
            String response = sendPostRequestWithJson("http://www.cxsensor.com/api/login");
            System.out.println("Response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String sendGetRequest(String url) throws IOException {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        // 创建HttpGet请求
        HttpGet request = new HttpGet(url);
        
        // 执行请求并获取响应
        HttpResponse response = httpClient.execute(request);
        
        // 将响应内容转换为字符串
        String result = EntityUtils.toString(response.getEntity());
        
        // 关闭HttpClient
        httpClient.close();
        
        return result;
    }
    public static String sendPostRequestWithJson(String url) throws IOException {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("username", "smartet");
        jsonMap.put("password", "123456");

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(jsonMap);
        // 创建HttpClient对象
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

            // 创建HttpPost请求
            HttpPost request = new HttpPost(url);

            // 创建JSON字符串作为请求体
            StringEntity entity = new StringEntity(json, "UTF-8");

            // 设置请求体的Content-Type为application/json
            entity.setContentType("application/json");

            // 将JSON字符串设置到请求中
            request.setEntity(entity);

            // 执行请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {

                // 将响应内容转换为字符串
                HttpEntity responseEntity = response.getEntity();
                String result = EntityUtils.toString(responseEntity, "UTF-8");

                // 返回结果
                return result;
            }
        }
    }
}
